import Cesium, { BaseLayerPicker } from '../Ces/Cesium'

import { We } from "../WeatherEarth"

class WeBaseLayerPickerWrapper {
  private _selectedImageryChangeEvent = new Cesium.Event
  private _selectedTerrainChangeEvent = new Cesium.Event
  private _selectedImageryIndex: number = 0
  private _selectedTerrainIndex: number = 0
  private _baseLayerPicker: BaseLayerPicker
  constructor(WE: We) {
    const { baseLayerPicker } = WE.viewer
    if (!baseLayerPicker) return
    this._baseLayerPicker = baseLayerPicker
    const { viewModel } = baseLayerPicker
    const getSelectedImagery =
      Object.getOwnPropertyDescriptor(viewModel, 'selectedImagery')?.get || (() => { })
    const setSelectedImagery =
      Object.getOwnPropertyDescriptor(viewModel, 'selectedImagery')?.set || (() => { })

    const getSelectedTerrain =
      Object.getOwnPropertyDescriptor(viewModel, 'selectedTerrain')?.get || (() => { })
    const setSelectedTerrain =
      Object.getOwnPropertyDescriptor(viewModel, 'selectedTerrain')?.set || (() => { })

    Object.defineProperty(viewModel, 'selectedImagery', {
      get: () => {
        return getSelectedImagery()
      },
      set: (value) => {
        for (let i = 0; i < viewModel.imageryProviderViewModels.length; i++) {
          if (value === viewModel.imageryProviderViewModels[i]) {
            this._selectedImageryIndex = i
            this._selectedImageryChangeEvent.raiseEvent(this, i)
            break
          }
        }
        setSelectedImagery(value)
      },
    })

    Object.defineProperty(viewModel, 'selectedTerrain', {
      get: () => {
        return getSelectedTerrain()
      },
      set: (value) => {
        for (let i = 0; i < viewModel.terrainProviderViewModels.length; i++) {
          if (value === viewModel.terrainProviderViewModels[i]) {
            this._selectedTerrainIndex = i
            this._selectedTerrainChangeEvent.raiseEvent(this, i)
            break
          }
        }
        setSelectedTerrain(value)
      },
    })
  }

  set selectedImageryIndex(value) {
    if (this._selectedImageryIndex === value || !this._baseLayerPicker) return
    const { viewModel } = this._baseLayerPicker
    if (value >= viewModel.imageryProviderViewModels.length) return
    viewModel.selectedImagery = viewModel.imageryProviderViewModels[value]
    this._selectedImageryIndex = value
  }

  get selectedImageryIndex() {
    return this._selectedImageryIndex
  }

  get selectedImageryChangeEvent() {
    return this._selectedImageryChangeEvent
  }

  set selectedTerrainIndex(value) {
    if (this._selectedTerrainIndex === value || !this._baseLayerPicker) return
    const { viewModel } = this._baseLayerPicker
    if (value >= viewModel.terrainProviderViewModels.length) return
    viewModel.selectedTerrain = viewModel.terrainProviderViewModels[value]
    this._selectedTerrainIndex = value
  }

  get selectedTerrainIndex() {
    return this._selectedImageryIndex
  }

  get selectedTerrainChangeEvent() {
    return this._selectedTerrainChangeEvent
  }
}

export default WeBaseLayerPickerWrapper

